home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / trace.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  5KB  |  236 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "global.h"
  4. #include "config.h"
  5. #include "mbuf.h"
  6. #include "iface.h"
  7. #include "trace.h"
  8. #include "session.h"
  9. #ifdef    UNIX
  10. #include <memory.h>
  11. #endif
  12.  
  13. /* Redefined here so that programs calling dump in the library won't pull
  14.  * in the rest of the package
  15.  */
  16. static char nospace[] = "No space!!\n";
  17. static char nullpak[] = "empty packet!!\n";
  18. static char if_tr_i[] = "%s recv: [%s]\n";
  19. static char if_tr_o[] = "%s sent: [%s]\n";
  20.  
  21. FILE *trfp = stdout;            /* file pointer used for tracing */
  22. int trcount = 0;                /* used to close file for flushing */
  23. char trname[40];                /* name if not stdout */
  24. int notraceall;            /* 0 = trace all, 1 = only in cmd mode */
  25. extern int mode;        /* command mode or not */
  26.  
  27. dump(interface,direction,type,bp)
  28. register struct interface *interface;
  29. int direction;
  30. unsigned type;
  31. struct mbuf *bp;
  32. {
  33.     struct mbuf *tbp;
  34.     void ascii_dump(),hex_dump();
  35.     int ax25_dump(),ether_dump(),ip_dump(),at_dump(),slfp_dump();
  36.     int (*func)();
  37.     char *cp;
  38.     long t, time();
  39.     int16 size;
  40.  
  41.     if((interface->trace & direction) == 0)
  42.         return;    /* Nothing to trace */
  43.  
  44.     if (notraceall && mode != CMD_MODE)
  45.         return; /* No trace while in session, if mode */
  46.  
  47.     if(bp == NULLBUF || (size = len_mbuf(bp)) == 0)
  48.         return;
  49.  
  50. #ifdef SCREEN
  51.     (void)outscreen(1);        /* DG2KK: switch to trace screen */
  52. #endif
  53.     time(&t);            /* DG2KK: get time */
  54.     cp = ctime(&t);
  55.     rip(cp);
  56.  
  57.     switch(direction){
  58.     case IF_TRACE_IN:
  59.         fprintf(trfp,if_tr_i,interface->name,cp);
  60.         break;
  61.     case IF_TRACE_OUT:
  62.         fprintf(trfp,if_tr_o,interface->name,cp);
  63.         break;
  64.     }
  65.     if(bp == NULLBUF || (size = len_mbuf(bp)) == 0){
  66.         fprintf(trfp,nullpak);
  67.         goto trdone;
  68.     }
  69.     if(type < NTRACE)
  70.         func = tracef[type];
  71.     else
  72.         func = NULLFP;
  73.  
  74.     dup_p(&tbp,bp,0,size);
  75.     if(tbp == NULLBUF){
  76.         fprintf(trfp,nospace);
  77.         if (trfp != stdout)
  78.           printf(nospace);
  79.         goto trdone;
  80.     }
  81.     if(func != NULLFP)
  82.         (*func)(&tbp,1);
  83.     if(interface->trace & IF_TRACE_ASCII){
  84.         /* Dump only data portion of packet in ascii */
  85.         ascii_dump(&tbp);
  86.     } else if(interface->trace & IF_TRACE_HEX){
  87.         /* Dump entire packet in hex/ascii */
  88.         free_p(tbp);
  89.         dup_p(&tbp,bp,0,len_mbuf(bp));
  90.         if(tbp != NULLBUF)
  91.             hex_dump(&tbp);
  92.         else {
  93.             fprintf(trfp,nospace);
  94.             if (trfp != stdout)
  95.               printf(nospace);
  96.             }
  97.     }
  98.     free_p(tbp);
  99.  
  100. #ifdef SCREEN
  101.     (void)outscreen(0);        /* switch back to main screen */
  102. #endif
  103.  
  104.       trdone:
  105.     if(trfp != stdout) {
  106. #ifndef ATARI_ST
  107.       fflush(trfp);
  108. #endif
  109. #if (defined(MSDOS) || defined(ATARI_ST))
  110.       if (++trcount > 25) {
  111.         fclose(trfp);
  112.         trfp = fopen(trname,"a");
  113.         trcount = 0;
  114.       }
  115. #endif
  116.       if (trfp == NULLFILE || ferror(trfp)) {
  117.         printf("Error on %s - trace to console\n",trname);
  118.         if (trfp != NULLFILE && trfp != stdout)
  119.           fclose(trfp);
  120.         trfp = stdout;
  121.       }
  122.     }
  123.     fflush(stdout);
  124. }
  125.  
  126. /* Dump an mbuf in hex */
  127. void
  128. hex_dump(bpp)
  129. register struct mbuf **bpp;
  130. {
  131.     int16 n;
  132.     int16 address;
  133.     void fmtline();
  134.     char buf[16];
  135.  
  136.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  137.         return;
  138.  
  139.     address = 0;
  140.     while((n = pullup(bpp,buf,sizeof(buf))) != 0){
  141.         fmtline(address,buf,n);
  142.         address += n;
  143.     }
  144. }
  145. /* Dump an mbuf in ascii */
  146. void
  147. ascii_dump(bpp)
  148. register struct mbuf **bpp;
  149. {
  150.     char c;
  151.     register int16 tot;
  152.  
  153.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  154.         return;
  155.  
  156.     tot = 0;
  157.     while(pullup(bpp,&c,1) == 1){
  158.         if((tot % 64) == 0) {
  159.             fprintf(trfp,"%04x  ",tot);
  160.         }
  161. #ifdef PC9801
  162.         if ((c >= 0x20) || (c < 0)) {
  163. #else
  164.         if(isprint(c)) {
  165. #endif
  166.             putchar(c);
  167.         } else {
  168.             putchar('.');
  169.         }
  170.         tot++;
  171.         if((tot % 64) == 0) {
  172. #ifdef PC9801
  173.             fprintf(trfp," \n");
  174. #else
  175.             fprintf(trfp,"\n");
  176. #endif
  177.         }
  178.     }
  179.     if((tot % 64) != 0) {
  180. #ifdef PC9801
  181.         fprintf(trfp," \n");
  182. #else
  183.         fprintf(trfp,"\n");
  184. #endif
  185.     }
  186. }
  187.  
  188. /* Print a buffer up to 16 bytes long in formatted hex with ascii
  189.  * translation, e.g.,
  190.  * 0000: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f  0123456789:;<=>?
  191.  */
  192. void
  193. fmtline(addr,buf,len)
  194. int16 addr;
  195. char *buf;
  196. int16 len;
  197. {
  198.     char line[80];
  199.     register char *aptr,*cptr;
  200.     register int16 c;
  201.     void ctohex();
  202.  
  203.     memset(line,' ',sizeof(line));
  204.     ctohex(line,(int16)hibyte(addr));
  205.     ctohex(line+2,(int16)lobyte(addr));
  206.     aptr = &line[6];
  207.     cptr = &line[55];
  208.     while(len-- != 0){
  209.         c = *buf++ & 0xff;
  210.         ctohex(aptr,c);
  211.         aptr += 3;
  212.         c &= 0x7f;
  213.         *cptr++ = isprint(c) ? c : '.';
  214.     }
  215. #ifdef PC9801
  216.     *cptr++ = ' ';
  217. #endif
  218.     *cptr++ = '\r';
  219.     *cptr++ = '\n';
  220.     fwrite(line,1,(unsigned)(cptr-line),stdout);
  221.     /* added */
  222. }
  223. /* Convert byte to two ascii-hex characters */
  224. static
  225. void
  226. ctohex(buf,c)
  227. register char *buf;
  228. register int16 c;
  229. {
  230.     static char hex[] = "0123456789abcdef";
  231.  
  232.     buf[0] = hex[hinibble(c)];
  233.     buf[1] = hex[lonibble(c)];
  234. }
  235.  
  236.